package webcrawler;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

/**
 * This class executes the Web Crawler.
 * 
 * @author Zach Turk, Dickson Kwong, Stephen Amos
 */
public class Main {

	/**
	 * The main() method to run the web crawler.
	 * 
	 * @param args Main args.
	 * @throws MalformedURLException 
	 */
	public static void main(String[] args) throws MalformedURLException {
		
		String user_input;
		// keywords to look for.
		String words[];
		String temp[] = new String[100];
		int word_count = 0;
		int page_limit = 0;
		int thread_num = 10;
		
		Scanner in = new Scanner(System.in);
		
		intro();
		
		
		//Check for key words
		System.out.print("Default Keywords? [y/n]");
		
		if (in.nextLine().equals("y")){
			words = new String[7];
			words[0] = "intelligence";
			words[1] = "artificial";
			words[2] = "agent";
			words[3] = "university";
			words[4] = "research";
			words[5] = "science";
			words[6] = "robot";
			word_count = 7;
		}
		else{
			System.out.println("Please enter the keywords one by one, enter '!' to end.");
			
			while (word_count == 0 || !temp[word_count - 1].equalsIgnoreCase("!"))
			{
				System.out.print("Word " + (word_count + 1) + ": ");
				temp[word_count] = in.nextLine();
				if(!temp[word_count].isEmpty())
					word_count++;
			}
			word_count--;
			words = new String[word_count];
			for(int i = 0; i < word_count; i++){
				words[i] = temp[i];
			}
		}
		
		//Check for thread mode
		System.out.print("Single thread? [y/n]");
		
		if (in.nextLine().equals("y")){
			thread_num = 1;
		} else {
			thread_num = thread();
		}
		
		//Check for web address
		user_input = web_address();
		
		//Check for page limit
		page_limit = page_limit();
		
		
		//Check for debug mode
		System.out.print("Debug mode? [y/n]");
		
		boolean debug = false;
		if (in.nextLine().equals("y"))
			debug = true;		
		
		System.out.println(user_input);
		System.out.println("Parsing: " + user_input);


		DataGatherer gatherer = new DataGatherer(user_input, words, page_limit, thread_num, debug);
		gatherer.run();
	}
	
	private static void intro() {
		System.out.println("      ====================================================");
		System.out.println("      |                Web Crawler v 1.2                 |");
		System.out.println("      |                Group: 0xDEADBEEF                 |");
		System.out.println("      |                      Dickson                     |");
		System.out.println("      |                      Stephen                     |");
		System.out.println("      |                      Zachary                     |");
		System.out.println("      ====================================================");
	}
	
	private static int page_limit() {
		
		int page_num = 1;
		boolean status = true;
		Scanner in = new Scanner(System.in);
		
		while(status || !(page_num > 0)){
			try {
				status = false;
				if (page_num > 0)
					System.out.print("Please enter the page limit: ");
				else
					System.out.print("Please enter a positive page limit: ");
				page_num = Integer.parseInt(in.nextLine());
			}
			catch(NumberFormatException nFE) {
				status = true;
			    System.out.println("Not an Integer, please re-enter...");
			}
		}
		return(page_num);
	}
	
	private static String web_address() throws MalformedURLException{
		
		Scanner in = new Scanner(System.in);
		String url = "http://faculty.washington.edu/gmobus/";
		String url_check = "";
		System.out.print("Default web address? [y/n]");
		
		if (!in.nextLine().equals("y")){
			while(url_check.equals("") || !isValid(url_check)){
				try {
					System.out.println("Please enter the web address.");
					
					System.out.print("Web Address: ");
					url_check = in.nextLine();
				}
				catch(NumberFormatException nFE) {
				    System.out.println("Not a correct URL, please re-enter...");
				}
			}
		}
		if (url_check != "")
			url = url_check;
		return (url);
	}
	
	private static int thread() {
		
		Scanner in = new Scanner(System.in);
		int thread_num = 1;
		boolean status = true;
		while(status || !(thread_num > 0)){
			try {
				status = false;
				if (thread_num > 0)
					System.out.print("Please enter the number of thread: ");
				else
					System.out.print("Please enter a positive number of thread: ");
				thread_num = Integer.parseInt(in.nextLine());
				
			}
			catch(NumberFormatException nFE) {
				status = true;
			    System.out.println("Not an valid Integer, please re-enter...");
			}
		}
		return(thread_num);
	}
	private static boolean isValid(final String the_url) throws MalformedURLException {

		boolean result = true;
		String urlStr = the_url;
		try {
		    // Get the image
		    URL url = new URL(urlStr);
		    InputStream is = url.openStream();
		    is.close();
		} catch (MalformedURLException e) {
		    // Print out the exception that occurred
			result = false;
		    System.out.println("Invalid URL "+urlStr+": "+e.getMessage());
		} catch (IOException e) {
		    // Print out the exception that occurred
			result = false;
		    System.out.println("Unable to execute "+urlStr+": "+e.getMessage());
		}
		
		return result;
	}
}
